package com.bawei.a2010douyin.ui.editMe;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bawei.a2010douyin.R;
import com.bawei.a2010douyin.base.mvp.BaseMvpActivity;
import com.bawei.a2010douyin.base.utils.ARouterPath;
import com.bawei.a2010douyin.base.utils.GlideUtils;
import com.bawei.a2010douyin.event.Event;
import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

@Route(path = ARouterPath.EDIT_AROUTER_PATH)
public class EditActivity extends BaseMvpActivity<EditMeContract.IditeMeView,EditPresenter> implements EditMeContract.IditeMeView {
    private ImageView iv;
    private TextView tv;
    @Override
    public int bindLayout() {
        return R.layout.activity_edit;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        iv = (ImageView) findViewById(R.id.iv);
        tv = (TextView) findViewById(R.id.tv);
    }

    @Override
    public EditPresenter createPresenter() {
        return new EditPresenter();
    }

    @Override
    public void initData() {

    }
    //上传服务器成功
    @Override
    public void showUpdateIcon(String data) {
        Toast.makeText(this, "山川成功", Toast.LENGTH_SHORT).show();
        GlideUtils.getInstance().loadCircleImage(this,data,iv);

        //发送广播
        Event event = new Event();
        event.data = data;//数据
        event.target = "MeFragment";//目标
        event.type = Event.TYPE_ICON;//类型：修改头像
        EventBus.getDefault().postSticky(event);




    }
    //点击事件：打开相册
    public void openCamera(View view) {
        PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture();
    }
    //挑选照片结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null){
            //获得图片路径
            Bundle bundle = data.getExtras();
            PictureBean pictureBean = (PictureBean) bundle.getParcelable(PictureSelector.PICTURE_RESULT);
            String path = pictureBean.getPath();
            //将此图片上传到服务器
            File file = new File(path);
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            //将requestBody封装成 MultipartBody.Part,注意必须用createFormData方法 ，注意参数二给文件名称
            //参数1 "file" 参数2：文件名字
            MultipartBody.Part part = MultipartBody.Part.createFormData("file",file.getName(),requestBody);
            mPresenter.updateIcon(part);





        }

    }

    public void changeNickName(View view) {
        //发送广播
        Event event = new Event();
        event.data = "有事请留言 留言吃的贼胖";//数据
        event.target = "MeFragment";//目标
        event.type = Event.TYPE_NICKNAME;//类型：修改昵称
        EventBus.getDefault().postSticky(event);
        finish();

    }
}